{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 项目描述："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "企业的快速发展离不开人才的支撑，可是现在我国的企业的人才流失严重，人才流失问题现在已经成为了关系企业发展的一个重大的问题。这些企业要想在目前激烈的竞争中快速发展，就需要依靠自身的人力资源的来竞争。只有拥有比对方更强，更优秀，更具有创造力的人才，才能在竞争中取得优势。所以如何有效解决我国企业人才流失问题是一个很迫切的任务。人才流失已经成了很多企业正在面临的困境，关键人才的流程对企业的影响尤为明显。\t无论在IT互联网领域还是传统领域、事业单位，均面临关键人才的流失，作为公司的核心的人力资源部门，我们需要把控员工的基本情况，对员工的情况进行实时监控和预测，人才流失模型从公司的角度和员工自身角度分别入手，阐释了在那些重要维度能够保持流失率的下降，常规的做法比如增强企业文化，提高薪资，提高年终奖等，通过模型给出人力资源部门一定的建议。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 技术说明："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "项目通过公司内部人资资源系统数据，通过数据的基本ETL(数据清洗过滤和汇总)对数据进行基本的预处理，通过python的numpy、pandas、matplotlib和seaborn进行各维度数据分析，经过数据分析得到分类特征较好的特征数据，对数值型数据、类别型数据、有序性数据分别进行处理和分析，使用label encoder和one encoder分别对类别数据进行特征编码，处理组合后的数据特征后形成特征向量，通过Python的Scikit-learn机器学习库的机器学习算法寻找数据之间存在的关系，从而为公司人力资源及决策层提供信息建议及决策建议。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. 需求分析："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "1 分析各个维度的数据对人才流失的影响\n",
    "\n",
    "2 通过训练数据建立的模型以及所给的测试数据，构建人才流失模型，最终预测测试数据相应的员工是否已经离职（0未离职，1离职）。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4. 数据集描述:\n",
    "\n",
    "数据主要包括影响员工离职的各种因素（工资、出差、工作环境满意度、工作投入度、是否加班、是否升职、工资提升比例等）以及员工是否已经离职的对应记录。数据分为训练数据和测试数据，分别保存在train.csv和test.csv两个文件中。训练数据主要包括1100条记录，31个字段。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Attrition：员工是否已经离职，1表示已经离职，0表示未离职，这是目标预测值；\n",
    "* Age：员工年龄\n",
    "* BusinessTravel：商务差旅频率，Non-Travel表示不出差，Travel_Rarely表示不经常出差，Travel_Frequently表示经常出差；\n",
    "* Department：员工所在部门，Sales表示销售部，Research & Development表示研发部，Human Resources表示人力资源部；\n",
    "* DistanceFromHome：公司跟家庭住址的距离，从1到29，1表示最近，29表示最远；\n",
    "* Education：员工的教育程度，从1到5，5表示教育程度最高；\n",
    "* EducationField：员工所学习的专业领域，Life Sciences表示生命科学，Medical表示医疗，Marketing表示市场营销，Technical Degree表示技术学位，Human Resources表示人力资源，Other表示其他；\n",
    "* EmployeeNumber：员工号码；\n",
    "* EnvironmentSatisfaction：员工对于工作环境的满意程度，从1到4，1的满意程度最低，4的满意程度最高；\n",
    "* Gender：员工性别，Male表示男性，Female表示女性；\n",
    "* JobInvolvement：员工工作投入度，从1到4，1为投入度最低，4为投入度最高；\n",
    "* JobLevel：职业级别，从1到5，1为最低级别，5为最高级别；\n",
    "* JobRole：工作角色：Sales Executive是销售主管，Research Scientist是科学研究员，Laboratory Technician实验室技术员，Manufacturing Director是制造总监，Healthcare Representative是医疗代表，Manager是经理，Sales Representative是销售代表，Research Director是研究总监，Human Resources是人力资源；\n",
    "* JobSatisfaction：工作满意度，从1到4，1代表满意程度最低，4代表满意程度最高；\n",
    "* MaritalStatus：员工婚姻状况，Single代表单身，Married代表已婚，Divorced代表离婚；\n",
    "* MonthlyIncome：员工月收入，范围在1009到19999之间；\n",
    "* NumCompaniesWorked：员工曾经工作过的公司数；\n",
    "* Over18：年龄是否超过18岁；\n",
    "* OverTime：是否加班，Yes表示加班，No表示不加班；\n",
    "* PercentSalaryHike：工资提高的百分比；\n",
    "* PerformanceRating：绩效评估；\n",
    "* RelationshipSatisfaction：关系满意度，从1到4，1表示满意度最低，4表示满意度最高；\n",
    "* StandardHours：标准工时；\n",
    "* StockOptionLevel：股票期权水平；\n",
    "* TotalWorkingYears：总工龄；\n",
    "* TrainingTimesLastYear：上一年的培训时长，从0到6，0表示没有培训，6表示培训时间最长；\n",
    "* WorkLifeBalance：工作与生活平衡程度，从1到4，1表示平衡程度最低，4表示平衡程度最高；\n",
    "* YearsAtCompany：在目前公司工作年数；\n",
    "* YearsInCurrentRole：在目前工作职责的工作年数\n",
    "* YearsSinceLastPromotion：距离上次升职时长\n",
    "* YearsWithCurrManager：跟目前的管理者共事年数；"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5. 流程分析"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. 数据获取(来源于公司内部的人力资源数据，通常没有缺失值的)\n",
    "2. 数据探索性分析\n",
    "3. 数据预处理\n",
    "4. 特征处理\n",
    "5. 数据集的划分：使用20%部分作为测试集，80%作为训练集\n",
    "6. 模型训练：逻辑回归、决策树、随机森林等\n",
    "7. 模型校验：模型准确率、召回率、精确率、F1值、ROC曲线(横轴：真正率TRP，纵轴：假正率FPR)-----通过曲线和x轴围城的面积衡量分类性能的好坏，曲线面积叫做AUC值---面积大小代表准确率大小---Roc-Auc曲线\n",
    "8. 使用imblearn框架进一步采样过采样或欠采样或结合方式或集成采样方式对\n",
    "9. 类别不均衡的问题进一步处理\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 6. 代码实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取CSV文件（内存优化关键步骤）\n",
    "# 使用低内存模式并指定数据类型（基于Evidence 7/17/20）\n",
    "dtypes = {   \n",
    "    'YearsWithCurrManager': 'float32',  \n",
    "    'YearsAtCompany': 'float32',  \n",
    "    'JobSatisfaction': 'float32',\n",
    "    'EnvironmentSatisfaction': 'float32',\n",
    "    'RelationshipSatisfaction': 'float32',\n",
    "    'WorkLifeBalance': 'float32',\n",
    "    'JobInvolvement': 'float32',\n",
    "    'JobSatisfaction': 'float32',\n",
    "    'NumCompaniesWorked': 'float32' \n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('../data/train.csv', \n",
    "                 encoding='utf-8',  # 处理特殊字符（Evidence 3）\n",
    "                 dtype=dtypes,           # 优化内存（Evidence 7）\n",
    "                 usecols=['YearsWithCurrManager','YearsAtCompany','JobSatisfaction','EnvironmentSatisfaction','RelationshipSatisfaction','WorkLifeBalance','JobInvolvement','JobSatisfaction','NumCompaniesWorked'],# 只加载必要列（Evidence 20）\n",
    "                 engine='c')             # 使用C引擎加快读取速度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1100 entries, 0 to 1099\n",
      "Data columns (total 8 columns):\n",
      " #   Column                    Non-Null Count  Dtype  \n",
      "---  ------                    --------------  -----  \n",
      " 0   EnvironmentSatisfaction   1100 non-null   float32\n",
      " 1   JobInvolvement            1100 non-null   float32\n",
      " 2   JobSatisfaction           1100 non-null   float32\n",
      " 3   NumCompaniesWorked        1100 non-null   float32\n",
      " 4   RelationshipSatisfaction  1100 non-null   float32\n",
      " 5   WorkLifeBalance           1100 non-null   float32\n",
      " 6   YearsAtCompany            1100 non-null   float32\n",
      " 7   YearsWithCurrManager      1100 non-null   float32\n",
      "dtypes: float32(8)\n",
      "memory usage: 34.5 KB\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算经理稳定性（与当前经理共事年限/在公司工作年限）。\n",
    "df['ManagerStability'] = df['YearsWithCurrManager'].div(df['YearsAtCompany']+0.001)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算综合满意度（工作满意度×0.3 + 工作环境满意度×0.2 + 人际关系满意度×0.2 + 工作生活平衡度×0.15 + 工作投入度×0.15）\n",
    "df['JobSatisfactionScore'] = (df['JobSatisfaction'] * 0.3 + \n",
    "                  df['EnvironmentSatisfaction'] * 0.2 + \n",
    "                  df['RelationshipSatisfaction'] * 0.2 + \n",
    "                  df['WorkLifeBalance'] * 0.15 + \n",
    "                  df['JobInvolvement'] * 0.15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算员工稳定性（工作满意度/（呆过公司数量+））\n",
    "df['EmployeeStability'] = (df['JobSatisfaction'] / df['NumCompaniesWorked']+1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1100 entries, 0 to 1099\n",
      "Data columns (total 11 columns):\n",
      " #   Column                    Non-Null Count  Dtype  \n",
      "---  ------                    --------------  -----  \n",
      " 0   EnvironmentSatisfaction   1100 non-null   float32\n",
      " 1   JobInvolvement            1100 non-null   float32\n",
      " 2   JobSatisfaction           1100 non-null   float32\n",
      " 3   NumCompaniesWorked        1100 non-null   float32\n",
      " 4   RelationshipSatisfaction  1100 non-null   float32\n",
      " 5   WorkLifeBalance           1100 non-null   float32\n",
      " 6   YearsAtCompany            1100 non-null   float32\n",
      " 7   YearsWithCurrManager      1100 non-null   float32\n",
      " 8   ManagerStability          1100 non-null   float32\n",
      " 9   JobSatisfactionScore      1100 non-null   float32\n",
      " 10  EmployeeStability         1100 non-null   float32\n",
      "dtypes: float32(11)\n",
      "memory usage: 47.4 KB\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ManagerStability</th>\n",
       "      <th>JobSatisfactionScore</th>\n",
       "      <th>EmployeeStability</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.999857</td>\n",
       "      <td>2.60</td>\n",
       "      <td>4.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.799840</td>\n",
       "      <td>2.50</td>\n",
       "      <td>1.428571</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.666593</td>\n",
       "      <td>2.50</td>\n",
       "      <td>4.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.380934</td>\n",
       "      <td>3.35</td>\n",
       "      <td>5.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.75</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1095</th>\n",
       "      <td>0.999500</td>\n",
       "      <td>2.40</td>\n",
       "      <td>1.333333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1096</th>\n",
       "      <td>0.749813</td>\n",
       "      <td>2.25</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1097</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>3.25</td>\n",
       "      <td>1.750000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1098</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.50</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1099</th>\n",
       "      <td>0.666445</td>\n",
       "      <td>1.80</td>\n",
       "      <td>1.166667</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1100 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      ManagerStability  JobSatisfactionScore  EmployeeStability\n",
       "0             0.999857                  2.60           4.000000\n",
       "1             0.799840                  2.50           1.428571\n",
       "2             0.666593                  2.50           4.000000\n",
       "3             0.380934                  3.35           5.000000\n",
       "4             0.000000                  1.75           3.000000\n",
       "...                ...                   ...                ...\n",
       "1095          0.999500                  2.40           1.333333\n",
       "1096          0.749813                  2.25           3.000000\n",
       "1097          0.000000                  3.25           1.750000\n",
       "1098          0.000000                  2.50           3.000000\n",
       "1099          0.666445                  1.80           1.166667\n",
       "\n",
       "[1100 rows x 3 columns]"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 提取features\n",
    "selected_features = df[['ManagerStability', 'JobSatisfactionScore','EmployeeStability']]\n",
    "selected_features\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ai_test",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
